package com.hgkj.cloud.base.service.impl;

import com.hgkj.cloud.base.dao.TenantFrameworksMapper;
import com.hgkj.cloud.base.domain.TenantFrameworks;
import com.hgkj.cloud.base.service.TenantFrameworksService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TenantFrameworksServiceImpl implements TenantFrameworksService {

    @Autowired
    private TenantFrameworksMapper tenantFrameworksMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return tenantFrameworksMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TenantFrameworks record) {
        return tenantFrameworksMapper.insert(record);
    }

    @Override
    public int insertSelective(TenantFrameworks record) {
        return tenantFrameworksMapper.insertSelective(record);
    }

    @Override
    public TenantFrameworks selectByPrimaryKey(Long id) {
        return tenantFrameworksMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TenantFrameworks record) {
        return tenantFrameworksMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TenantFrameworks record) {
        return tenantFrameworksMapper.updateByPrimaryKey(record);
    }

    @Override
    public TenantFrameworks getFrameworkByName(Map<String, Object> map) {
        return tenantFrameworksMapper.getFrameworkByName(map);
    }

    @Override
    public List<TenantFrameworks> getFrameworkByFatherid(Map<String, Object> map) {
        return tenantFrameworksMapper.getFrameworkByFatherid(map);
    }

    @Override
    public List<TenantFrameworks> getFrameworkByMap(Map<String, Object> map) {
        return tenantFrameworksMapper.getFrameworkByMap(map);
    }

    @Override
    public TenantFrameworks getFrameworkById(Long id) {
        return tenantFrameworksMapper.getFrameworkById(id);
    }

    @Override
    public List<TenantFrameworks> listAllFramework(Map<String, Object> map) {
        return tenantFrameworksMapper.listAllFramework(map);
    }

    @Override
    public int delFramework(Long id) {
        return tenantFrameworksMapper.delFramework(id);
    }

    @Override
    public List<TenantFrameworks> listFrameworksByTaskuuid(Map<String, Object> map) {
        return tenantFrameworksMapper.listFrameworksByTaskuuid(map);
    }

    @Override
    public List<TenantFrameworks> listFrameworksByQnTaskid(Map<String, Object> map) {
        return tenantFrameworksMapper.listFrameworksByQnTaskid(map);
    }

    @Override
    public List<TenantFrameworks> listAllFrameworkByTenantId(Map<String, Object> map) {
        return tenantFrameworksMapper.listAllFrameworkByTenantId(map);
    }

    @Override
    public List<TenantFrameworks> getFramByTenant(Map map) {
        return tenantFrameworksMapper.getFramByTenant(map);
    }

    @Override
    public int updateFrameworkCreateDate(TenantFrameworks string) {
        return tenantFrameworksMapper.updateFrameworkCreateDate(string);
    }

    @Override
    public int deleteFrameworkCreateDate() {
        return tenantFrameworksMapper.deleteFrameworkCreateDate();
    }

    @Override
    public TenantFrameworks getFrameworkDataByMap(Map<String, Object> map) {
        return tenantFrameworksMapper.getFrameworkDataByMap(map);
    }

    @Override
    public List<TenantFrameworks> listAllFrameworkByMap(Map<String, Object> map) {
        return tenantFrameworksMapper.listAllFrameworkByMap(map);
    }

    @Override
    public List<TenantFrameworks> listAllSonFrameworkByFatherId(Long fatherId) {
        return tenantFrameworksMapper.listAllSonFrameworkByFatherId(fatherId);
    }

    @Override
    public long listAllFrameworkCountByMap(Map<String, Object> map) {
        return tenantFrameworksMapper.listAllFrameworkCountByMap(map);
    }

    public List<TenantFrameworks> listAllTenantFrameworksByParentAndLevel(Map<String, Object> map) {
        List<TenantFrameworks> list = new ArrayList<>();
        TenantFrameworks parent;
        if (map.get("parentId") != null) {
            long parentId = Long.parseLong(map.get("parentId").toString());
            parent = tenantFrameworksMapper.getFrameworkById(parentId);
        } else {
            String parentWholeName = map.get("parentWholeName").toString();
            parent = tenantFrameworksMapper.getFrameworkByName(
                    new HashMap<String, Object>() {{
                        put("whole_frame_name", parentWholeName);
                    }});
        }
        if (parent == null) {
            return list;
        }
        int level = Integer.parseInt(map.get("level").toString());
        boolean includeSelf = Boolean.parseBoolean(map.get("includeSelf").toString());
        if (includeSelf) {
            list.add(parent);
        }
        if (level <= 0) {
            return list;
        }
        List<TenantFrameworks> children;
        if (level == 999) {
            Map<String, Object> newMap = new HashMap<>();
            newMap.put("source_id", parent.getId());
            //把自己排除掉
            newMap.put("include_self", 0);
            children = tenantFrameworksMapper.listAllFrameworkByMap(newMap);
        } else {
            children = listAllChildrenByParentAndLevel(parent.getId(), level);
        }
        list.addAll(children);
        return list;
    }

    public List<TenantFrameworks> listAllChildrenByParentAndLevel(Long parentId, int level) {
        level--;
        List<TenantFrameworks> list = new ArrayList<>();
        List<TenantFrameworks> children = tenantFrameworksMapper.listAllSonFrameworkByFatherId(parentId);
        if (CollectionUtils.isEmpty(children)) {
            return list;
        }
        if (level < 1) {
            return children;
        }
        list.addAll(children);
        for (TenantFrameworks frameworks1 : children) {
            list.addAll(listAllChildrenByParentAndLevel(frameworks1.getId(), level));
        }
        return list;
    }
}
